package Server;
import java.net.*;
import java.io.*;

public class SocketManager {

	private Socket mySocket;
	private DataOutputStream bufferEscritura;
	private BufferedReader bufferLectura;

	public SocketManager(Socket sock) throws IOException {
		this.mySocket = sock;
		InicializaStreams();
	}

	/**
	 *
	 * @param address InetAddress
	 * @param port int numero de puerto
	 * @throws IOException
	 */
	public SocketManager(InetAddress address, int port) throws IOException {
		mySocket = new Socket(address, port);
		InicializaStreams();
	}

	/**
	 *
	 * @param host String nombre del servidor al que se conecta
	 * @param port int puerto de conexion
	 * @throws IOException
	 */
	public SocketManager(String host, int port) throws IOException {
		mySocket = new Socket(host, port);
		InicializaStreams();
	}

	/**
	 * Inicializaci�n de los bufferes de lectura y escritura del socket
	 * @throws IOException
	 */
	public void InicializaStreams() throws IOException {
		bufferEscritura = new DataOutputStream(mySocket.getOutputStream());
		bufferLectura = new BufferedReader(new InputStreamReader(mySocket.
				getInputStream()));
	}

	public void CerrarStreams() throws IOException {
		bufferEscritura.close();
		bufferLectura.close();
	}

	public void CerrarSocket() throws IOException {
		mySocket.close();
	}

	/**
	 *
	 * @return String
	 * @throws IOException
	 */
	public String Leer() throws IOException {
		return (bufferLectura.readLine());
	}
	
	public void Escribir(String contenido) throws IOException {
		bufferEscritura.writeBytes(contenido);
	}
	
	public void Escribir(byte[] buffer, int bytes) throws IOException {
		bufferEscritura.write(buffer, 0, bytes);
	}
	
	public void escribirBytes(byte[] array) throws IOException {
	    escribirBytes(array, 0, array.length);
	}

	public void escribirBytes(byte[] array, int inicio, int tamanyo) throws IOException {
	    if (tamanyo < 0 || inicio < 0 || inicio >= array.length)
	    {
	    	throw new IOException("Error, tama�o negativo");
	    }
	    OutputStream out = mySocket.getOutputStream(); 
	    DataOutputStream dos = new DataOutputStream(out);
	    
	    dos.writeInt(tamanyo);
	    if (tamanyo > 0)
	    {
	        dos.write(array, inicio, tamanyo);
	    }
	}
	
	public byte[] leerBytes() throws IOException {
	    InputStream in = mySocket.getInputStream();
	    DataInputStream dis = new DataInputStream(in);

	    int tamanyo = dis.readInt();
	    byte[] array = new byte[tamanyo];
	    if (tamanyo > 0)
	    {
	        dis.readFully(array);
	    }
	    return array;
	}
}
